﻿using Ideaa.Data.Infrastructure;
using PersistedModels = Ideaa.Data.Models;
using DomainModels = Ideaa.Domain.Models.Idea;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AutoMapper;

namespace Ideaa.Data.Repositories
{
    public interface IIdeaRepository
    {
        DomainModels.Idea GetById(int ideaId);

        void AddIdea(DomainModels.Idea idea, out PersistedModels.Idea persistedModel);
    }

    public class IdeaRepository : GenericRepository<PersistedModels.Idea>, IIdeaRepository
    {
        public IdeaRepository(DbContext context)
            : base(context)
        {

        }

        public DomainModels.Idea GetById(int ideaId)
        {
            DomainModels.Idea domainModel;
            PersistedModels.Idea persistedModel = base.GetById(ideaId);

            domainModel = Mapper.Map<DomainModels.Idea>(persistedModel);

            return domainModel;
        }

        public void AddIdea(DomainModels.Idea idea, out PersistedModels.Idea persistedModel)
        {
            persistedModel = Mapper.Map<PersistedModels.Idea>(idea);
            persistedModel = (PersistedModels.Idea) Mapper.Map(idea, idea.GetType(), typeof(PersistedModels.Idea));

            base.Add(persistedModel);
        }

        
    }
}
